package com.example.mybatisplus.controller;

import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.mapper.RegisterMapper;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class RegisterController {
    @Autowired
    RegisterMapper registerMapper;
    @Autowired
    public RegisterService registerService;

    @ResponseBody
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public JsonResponse<Object> response(@RequestBody User user){
        String stu_username = user.getStuUsername();
        String stu_password = user.getStuPassword();
        String stu_name = user.getStuName();
        String phone = user.getPhone();
        Integer age = user.getAge();
        Integer sex = user.getSex();
        System.out.println(registerMapper.queryUser(stu_username));
        System.out.println(stu_username);
        boolean isExist = registerService.queryUser(stu_username);
        System.out.println(isExist);
        System.out.println(stu_name);
        System.out.println(phone);
        System.out.println(age);
        System.out.println(sex);
//        System.out.println("\n");
//        System.out.println("\n");
//        System.out.println("下\n");
//        System.out.println(user);
        if(isExist){
            return JsonResponse.failure("已存在该用户");
        }else {
            registerService.register(stu_name,sex,phone,stu_username, stu_password,age);
            return JsonResponse.success().setMessage("成功");
        }

//        registerMapper.addUser(stu_username, stu_password);
    }

}
